import * as req from '@/request';

export async function getConfig(params: { [key: string]: any }) {
  const { productId } = params;
  if (productId == undefined) {
    return Promise.resolve({} as API.ProductAdConfig);
  }
  const result = await req.get<API.ProductAdConfigResult>(
    '/ad/getConfig',
    params,
  );

  if (result.success && result.data) {
    return Promise.resolve(result.data);
  }
  return Promise.resolve({} as API.ProductAdConfig);
}

export async function updateConfig(data: { [key: string]: any }) {
  return req.postJson<API.Result>('/ad/updateConfig', data);
}

export async function list(
  params: { [key: string]: any },
  sorter?: { [key: string]: any },
  filter?: { [key: string]: any },
) {
  return req.postForm<API.AdList>('/ad/list', params, sorter, filter);
}

export async function add(data: { [key: string]: any }) {
  return req.postJson<API.Result>('/ad/create', data);
}

export async function update(data: { [key: string]: any }) {
  return req.postJson<API.Result>('/ad/update', data);
}

export async function remove(data: { key: number }) {
  return req.post<API.Result>('/ad/delete', data);
}
